//
// Created by liushuai on 2021/1/25.
//

#include "getValue.h"
<<<<<<< HEAD:cxx/algorithm/src/getValue.cc
bool moreThanHalf(vector<int>& gifts,int n){
    int count = 0;
    for(auto e:gifts){
        if(e==n)
            count+=1;
    }
    if(count>gifts.size()/2)
        return true;
    else
        return false;

}
int getValue(vector<int> gifts, int n) {
    if (gifts.empty())
        return 0;
    int current = gifts[0];
    int count = 1;
    for (int i = 1; i < n; i++) {
        if (count == 0) {
            current = gifts[i];
            count = 1;
        } else if (gifts[i] == current)
            count++;
        else
            count--;
    }
    if(moreThanHalf(gifts,current))
        return current;
    else
        return 0;
=======
bool moreThanHalf(vector<int>& gifts, int n) {
  int count = 0;
  for (auto e : gifts) {
    if (e == n) count += 1;
  }
  if (count > gifts.size() / 2)
    return true;
  else
    return false;
}
int getValue(vector<int> gifts, int n) {
  if (gifts.empty()) return 0;
  int current = gifts[0];
  int count = 1;
  for (int i = 1; i < n; i++) {
    if (count == 0) {
      current = gifts[i];
      count = 1;
    } else if (gifts[i] == current)
      count++;
    else
      count--;
  }
  if (moreThanHalf(gifts, current))
    return current;
  else
    return 0;
>>>>>>> dev:cxx/algorithm/common/src/getValue.cc
}